package com.lonely.fx.bind;

import javafx.beans.binding.IntegerBinding;
import javafx.beans.property.SimpleIntegerProperty;

/**
 * @date: 2022-07-24 21:26
 * @author: ztkj-hzb
 * @description: 自定义绑定计算学习
 **/
public class CustomBindCalculationSettingMain {


    public static void main(String[] args) {

        //创建自定义绑定类实例
        MyIntegerBinding myIntegerBinding = new MyIntegerBinding(10);
        System.out.println(myIntegerBinding.get());

        //该实例同样可以跟其他Property进行绑定
        SimpleIntegerProperty simpleIntegerProperty = new SimpleIntegerProperty(20);
        simpleIntegerProperty.bind(myIntegerBinding);
        myIntegerBinding.setValue(100);
        System.out.println("myIntegerBinding: " + myIntegerBinding.get());
        System.out.println("simpleIntegerProperty: " + simpleIntegerProperty.get());
    }


    /**
     * 自定义绑定类
     */
    private static class MyIntegerBinding extends IntegerBinding {

        private SimpleIntegerProperty integerProperty = new SimpleIntegerProperty();

        public MyIntegerBinding(int x) {
            integerProperty.set(x);
        }

        public void setValue(int value) {
            this.integerProperty.set(value);
        }

        /**
         * 计算
         */
        @Override
        protected int computeValue() {
            //根据传入的值，返回双倍值
            return integerProperty.get() * 2;
        }
    }
}
